<div style="width:675px; float:left; padding-left:5px; margin-left:5px" class="border_radius">
<?php
	include_once("../../coms/xl_thongtin.php");
	$bn = new xl_thongtin();
	$arr_bn = $bn->Load_Banner();
	
	if(isset($_REQUEST['act']) && isset($_REQUEST['id'])):
		$act = $_REQUEST['act'];
		$id = $_REQUEST['id'];
		if($act == 'del' && is_numeric($id)):
			$ban = new xl_thongtin();
			$arr_bn = $ban->Load_Banner_Theo_ID($id);
			unlink("../../".$arr_bn[0]['hinhanh']."");
			$xoa = new xl_thongtin();
			$kq2 = $xoa->XoaBanner($id);			
			echo '<script type="text/javascript">
						alert("Xóa banner thành công!!!");	
						window.location="trangchu.php?key=qlbn";				
					</script>';
		endif;
	endif;		
?>


<div class="content_right_title_admin">
Quản lý banner
</div>      



<div class="content_right_group_admin box_shadow" style="padding-bottom:5px">
<form method="post" name="fr_themhinhanh" action="trangchu.php?key=qlbn" enctype="multipart/form-data" >
    <table width="60%" align="center"  cellpadding="0" cellspacing="0" class="border_radius" style="font-size:12px" >
        <tr>
            <td colspan="2" align="center" height="40px"><span style="font-size:18px; font-weight:bold; color:#f9d765; ">Thêm banner mới</span></td>
        </tr>
        <tr>
            <td width="30%" style="padding-left:5px;" height="30px" align="left">Tên hình ảnh<span style="color:#F00">(*)</span></td>
            <td align="left"><input type="text" name="txt_tenhinh" size="30" style="height:20px;" class="border_radius"></td>
        </tr>
        <tr>
            <td width="30%" style="padding-left:5px;" height="30px" align="left">Nội dung hình ảnh</td>
            <td align="left"><input type="text" name="txt_noidung" size="30" style="height:20px;" class="border_radius"></td>
        </tr>
        <tr>
            <td width="30%" style="padding-left:5px;" height="30px" align="left">Link</td>
            <td align="left"><input type="text" name="txt_link" size="30" style="height:20px;" class="border_radius"></td>
        </tr>
        <tr>
            <td style="padding-left:5px;" height="30px" align="left" valign="top">Đường dẫn<span style="color:#F00">(*)</span></td>
            <td align="left"><input type="file" name="up_hinh" size="30" style="height:20px;"  class="border_radius"><br />
            <span style="font-size:10px; color:#666">(Kích thước banner tối đa 408px x 200px)</span>
</td>
        </tr>     
        
        <tr>
            <td colspan="2" align="center" style="vertical-align:middle;">
                <input type="submit" name="btn_upload" value="Thêm mới" class="button small blue" style="margin-top:10px;"/>
            </td>
        </tr>
    </table>
</form>
<br />

<table width="100%" cellspacing="0" style="padding-left:5px; padding-right:5px">
	<tr bgcolor="#999">
    	<th align="center" valign="middle">
        	STT
        </th>
    	<th align="center" valign="middle">
        	Tên banner
        </th>
        <th align="center" valign="middle">
        	Thông tin banner
        </th>
        <th align="center" valign="middle">
        	Chức năng
        </th>
    </tr>
<?php
for ($i=0; $i<count($arr_bn); $i++):
	$row = $arr_bn[$i];
?>
    <tr style="border-bottom:#666;">
    	<td align="center" valign="middle">
        	<?php echo $i+1; ?>
        </td>
        <td align="center" valign="middle">
        	<?php echo $row['tenthongtin']; ?>
        </td>
    	<td align="center" valign="middle">
        	
            <table width="100%" cellpadding="0" cellspacing="0">
            	<tr>
                	<td style="color:#333">
                    	<img src="../../<?php echo $row['hinhanh']; ?>" border="0" width="400px"><br>
						- Nội dung: <?php echo $row['noidung']; ?><br>
						- Link: <a href="<?php echo $row['link']; ?>"><?php echo $row['link']; ?></a><br>						
                    </td>
                </tr>
            </table>
            
        </td>
        <td align="center" valign="middle">
       		<a href="trangchu.php?key=sbn&act=upd&malt=10&id=<?php echo $row['mathongtin']; ?>" class="button medium blue">Sửa</a>
        	<a href="trangchu.php?key=qlbn&act=del&malt=10&id=<?php echo $row['mathongtin']; ?>" class="button medium blue" onclick="javascript: return confirm('Bạn chắc muốn xóa banner này không?');">Xóa</a>
        </td>
    </tr>
<?php
endfor;
?>
</table>
</div>
</div>

<?php
 
if(isset($_POST['btn_upload']))
{
	$maalbum = 1;
	$tenhinhanh = $_POST['txt_tenhinh'];
	$noidunghinhanh = $_POST['txt_noidung'];
	$link = $_POST['txt_link'];
	$duongdan = "images/slide/".$_FILES['up_hinh']['name'];
	//$thumbnail = "hinhanhthicong/thumbnail/thumb_".$_FILES['up_hinh']['name'];	
	$kq=array();
	$upload = new xl_thongtin();
	if($duongdan != NULL)
	{
		//$hinh_thumb = "thumb_".$file;
		$kq=up_hinh();
		//$hinh = $file;
		//$hinh_thumb = "thumb_".$kq[1];
		if($kq[2]==true)
		{
			//echo $hinh_thumb."<br>";
			//echo $hinh;
			$now = getdate();
			$currentDate = $now["year"] . "-" . $now["mon"] . "-" . $now["mday"]; 
			//$hinh_thunb=make_thumbnails($kq[0], $kq[1]);
			$ketqua = $upload->ThemBanner($tenhinhanh, $noidunghinhanh, $link, $duongdan);
			echo '<meta http-equiv="refresh" content="0;url=trangchu.php?key=qlbn" />';
		}
	}
	if($duongdan == NULL)
	{
		echo '<script language="javascript">
				window.alert("Không có tập tin hình ảnh. Vui lòng chọn hình ảnh!!!");
			</script>';		
		echo '<meta http-equiv="refresh" content="0;url=trangchu.php?key=ga"  />';
	}
	
	
	
}

function up_hinh()
{
	$result = "";
	define('UPLOAD_DIR','../../images/slide/');
	define('UPLOAD_DIR_THUMBNAIL','../../images/slide/thumbnail/');
	define('MAX_FILES_SIZE',10000000);
	$file = str_replace(" ","_",$_FILES['up_hinh']['name']);
	//convert maximun size to KB
	$max = number_format(MAX_FILES_SIZE/1024,1)." KB";
	//create an array of permitted MIME types
	$premitted = array('image/gif','image/jpeg','image/pjpeg','image/png');
	//begin by assuming the file is unacceptable
	$sizeOK = false;
	$typeOK = false;
	$success = false;
	$kq=array(3);
	//Check that file is within the permitted size
	if($_FILES['up_hinh']['size']>0 && $_FILES['up_hinh']['size']<= MAX_FILES_SIZE)
	{
		$sizeOK = true;
	}
	
	//Check that file is within the permitted size
	foreach($premitted as $type)
	{
		if($type == $_FILES['up_hinh']['type'])
		{
			$typeOK = true;
			break;
		}
	}
	
	if($sizeOK && $typeOK)
	{
		switch($_FILES['up_hinh']['error'])
		{
			case 0:
				if(!file_exists(UPLOAD_DIR.$file))
				{
					//move the file to the upload folder and rename				
					$success = move_uploaded_file($_FILES['up_hinh']['tmp_name'],UPLOAD_DIR.$file);
					// the path with the file name where the file will be stored, upload is the directory name.
					/* $dir = UPLOAD_DIR;
					echo "<script>alert('".$dir."')</script>";
					echo "<script>alert('".$file."')</script>";
					$make_thumbnails($dir, $file); */
					//CreateThumbnail(UPLOAD_DIR.$file);
					$kq[1]=$file;
					$kq[0] = UPLOAD_DIR;
					
				}
				else
				{
					$now  = date("YmdHis",time());
					$file = $now.$file;
					$success = move_uploaded_file($_FILES['up_hinh']['tmp_name'],UPLOAD_DIR.$file);
					$kq[1]=$file;
					$kq[0] = UPLOAD_DIR;
					/* make_thumbnails(UPLOAD_DIR, $file); */
					//CreateThumbnail(UPLOAD_DIR.$now.$file);
				}
				if($success){
					$result = "$file upload thành công";
					//$result = "$file uploaded successfully";
				}
				else{
					$result = "Lỗi upload $file.Vui long thử lại.";
					//$result = "Error uploading $file.Please try again.";
				}
				break;
			case 3:
				$result = "Lỗi upload $file.Vui long thử lại.";
				//$result = "Error uploading $file.Please try again.";
			default:
				$result = "Lỗi hệ thống upload $file.Vui long thử lại.";
				//$result = "System error uploadding $file contact webmaster";
		}
	}
	elseif($_FILES['up_hinh']['error']==4){
		$result = "Chưa chọn file upload";
		//$result = "No file selected";
	}
	else{
		$result = "Lỗi file upload. Maximum size: $max. File upload phải là gif, jpg, png.";
		//$result ="$file cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, png.";
	}
	if($success == TRUE)
	{
		echo '<script language="javascript">
				window.alert("'.$result.'");
			</script>';
		//echo '<meta http-equiv="refresh" content="2;url=them_tintuc.php"  />';
	}
	else
	{
		echo '<script language="javascript">
				window.alert("'.$result.'");
			</script>';
		//echo '<meta http-equiv="refresh" content="2;url=them_tintuc.php"  />';
	}
	$kq[2] = $success;
	return $kq;
}

function make_thumbnails($updir, $img){
	
	$thumb_preword		= "thumb_";
	
	$arr_image_details	= GetImageSize("$updir"."$img");
	$original_width		= $arr_image_details[0];
	$original_height	= $arr_image_details[1];
	
	//$thumbnail_width	= 132;
	$thumbnail_height	= 96;
	$ratio = $thumbnail_height / $original_height;
    $thumbnail_width	= $original_width * $ratio;
	
	if( $original_width > $original_height ){
		$new_width	= $thumbnail_width;
		$new_height	= intval($original_height*$new_width/$original_width);
	} else {
		$new_height	= $thumbnail_height;
		$new_width	= intval($original_width*$new_height/$original_height);
	}

	$dest_x = intval(($thumbnail_width - $new_width) / 2);
	$dest_y = intval(($thumbnail_height - $new_height) / 2);
	
	

	if($arr_image_details[2]==1) { $imgt = "ImageGIF"; $imgcreatefrom = "ImageCreateFromGIF";  }
	if($arr_image_details[2]==2) { $imgt = "ImageJPEG"; $imgcreatefrom = "ImageCreateFromJPEG";  }
	if($arr_image_details[2]==3) { $imgt = "ImagePNG"; $imgcreatefrom = "ImageCreateFromPNG";  }


	if( $imgt ) { 
		$old_image	= $imgcreatefrom("$updir"."$img");
		$new_image	= imagecreatetruecolor($thumbnail_width, $thumbnail_height);
		imageCopyResized($new_image,$old_image,$dest_x, $dest_y,0,0,$new_width,$new_height,$original_width,$original_height);
		$imgt($new_image,UPLOAD_DIR_THUMBNAIL."$thumb_preword"."$img");
	}
	return "$thumb_preword"."$img";

}

		
?>